<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Handlers\PictuerAverageHandler;
use App\Models\Pictuer;
class GetPictuerAverageJobs implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $pictuers;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($pictuers)
    {
        $this->pictuers = $pictuers;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 执行抓取请求
        $averages = app(PictuerAverageHandler::class)->get($this->pictuers);
        // 批量写入
        foreach ($this->pictuers as $pictuer) {
          $pic = Pictuer::find($pictuer['id']);
          $pic->rgb = $averages[$pictuer['id']]['rgb'];
          $pic->save();
          $pic->setAverage($pic);
        }
    }
}
